package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.model.Advertisement;
import com.stylefeng.guns.common.persistence.model.OpUser;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.AdvertisementDao;
import com.stylefeng.guns.modular.system.dao.OpUserDao;
import com.stylefeng.guns.modular.system.warpper.AdvertisementWarpper;
import com.stylefeng.guns.modular.system.warpper.OpUserWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.naming.NoPermissionException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统广告控制器
 *
 * @author fengshuonan
 * @Date 2017年1月11日 下午1:08:17
 */
@Controller
@RequestMapping("/advertisement")
public class AdvertisementController extends BaseController {

    private static String PREFIX = "/system/advertisement/";

    @Resource
    private GunsProperties gunsProperties;

    @Resource
    private AdvertisementDao advertisementDao;

    /**
     * 跳转到查看广告列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "advertisement.html";
    }

    /**
     * 跳转到查看广告列表的页面
     */
    @RequestMapping("/advertisement_add")
    public String addView() {
        return PREFIX + "advertisement_add.html";
    }

    /**
     * 跳转到编辑广告页面
     */
    @RequestMapping("/advertisement_update/{id}")
    public String userEdit(@PathVariable Integer id, Model model) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Advertisement advertisement = this.advertisementDao.selectById(id);
        model.addAttribute("advertisement",advertisement);
        String state_name = "";
        if(advertisement.getState()==0){
            state_name = "启用";
        }else if(advertisement.getState()==1){
            state_name = "禁用";
        }
        model.addAttribute("state_name",state_name);
        LogObjectHolder.me().set(advertisement);
        return PREFIX + "advertisement_edit.html";
    }

    /**
     * 查询广告列表
     */
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) Integer type, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) {
        List<Map<String, Object>> list = advertisementDao.selectAdvertisements(type, beginTime, endTime);
        return new AdvertisementWarpper(list).warp();
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @BussinessLog(value = "添加", key = "account", dict = Dict.UserDict)
    @ResponseBody
    public Object add(Advertisement advertisement) {
        advertisement.setCreate_time(new Date());
        advertisement.setUpdate_time(new Date());
        this.advertisementDao.insert(advertisement);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     *
     * @throws NoPermissionException
     */
    @RequestMapping("/edit")
    @BussinessLog(value = "修改", key = "account", dict = Dict.UserDict)
    @ResponseBody
    public Object edit(Advertisement advertisement) {
        advertisement.setUpdate_time(new Date());
        advertisementDao.updateById(advertisement);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除（逻辑删除）
     */
    @RequestMapping("/delete")
    @BussinessLog(value = "删除", key = "userId", dict = Dict.UserDict)
    @ResponseBody
    public Object delete(@RequestParam Integer id) {

        advertisementDao.deleteById(id);

        return SUCCESS_TIP;
    }
}
